<%inherit file="/base/siteadmin.html"/>

<table> 
    %for hostname in c.siteadmin_clusters:
        <%
        tag_prefix = hostname.replace('.','-1-')
        jobs_tag = tag_prefix + '_jobs'
        table_tag = tag_prefix + '_table'
        %> 
        <tr class='nohover table_row_emph' align='center'> 
            <td colspan=2> ${hostname}</td> 
        </tr> 
        <tr class='nohover'>
            <td>
                <div id='${table_tag}'> </div>
            </td>
            <td> 
                <div id='${jobs_tag}'> </div>
            </td> 
        </tr>
    %endfor 
</table>

<script language="javascript" type="text/javascript">
    
google.load("visualization", "1", {packages:["corechart"]});
    
function drawClusterUserJobs( hostname, id_tag){
        var data, options, chart, _e, _height;
        var _url = '/json/jobs/gc_cuj_states/' + hostname;
    
        var json_str = $.ajax({
            url : _url,
            type : 'post',
            dataType: 'text',
            async: false
            }).responseText;

        data = new google.visualization.DataTable(json_str, 0.6);
        
        // some black-magic for plot heigh estimation
        _e  = json_str.length - 400 ;
        _height = ((_e - _e % 125) / 125) * 30 + 40 ;


        options = {
            colors: ['#4e9258', '#e41b00', '#993333', '#828282', '#6D98AB', '#EEF66C','#666699','#ffa500'],
            axisTitlesPosition : 'out',
            backgroundColor : '#ffffff',
            width : 500,
            height: _height, 
            hAxis : {
                baseline: 0
            },
            chartArea: {left: 60},
            isStacked : true,
            legend: 'top',
            title: 'User Jobs on ' + hostname
        };

    chart = new google.visualization.BarChart(document.getElementById(id_tag));
    chart.draw(data, options);
}



var generateLoadTable = function(hostname, id_tag){
    /* hostname - name of host to query
       id_tag - id of html tag to which to 'dump' generated table. 
    */

    var _html;
    var _zebra = false;
    var _url = '/json/jobs/get_cuj_states/' + hostname;

    $.ajax({
        url: _url,
        type: 'POST',
        dataType: 'json',
        success: function(data){
            _html = '<table width="500"> ' + 
                    '<tr class="table_head2"> <th class="gm"> User</th>' + 
                    '<th class="gm"> FIN</th> '+ 
                    '<th class="gm"> FAIL </th> ' + 
                    '<th class="gm"> KIL </th>' + 
                    '<th class="gm"> DEL</th>' + 
                    '<th class="gm"> FTCH</th>' + 
                    '<th class="gm"> RUN</th>' + 
                    '<th class="gm"> other </th>' + 
                    '<th class="gm"> orph </th>' + 
                    '<th class="gm"> total </th></tr>';
            
            for (var user in data){
                 if (_zebra){
                    _html +="<tr class='odd'>";
                    _zebra = false;
                }
                else{
                    _html +="<tr class='even'>";
                    _zebra = true;
                }
                _html += '<td>' + user.split('CN=')[1] + '</td>' ;
                _html += '<td>' + data[user].FINISHED + '</td>' ;
                _html += '<td>' + data[user].FAILED + '</td>' ;
                _html += '<td>' + data[user].KILLED + '</td>' ;
                _html += '<td>' + data[user].DELETED + '</td>' ;
                _html += '<td>' + data[user].FETCHED + '</td>' ;
                _html += '<td>' + data[user].RUN + '</td>' ;
                _html += '<td>' + data[user].other + '</td>' ;
                _html += '<td>' + data[user].orphaned + '</td>' ;
                _html += '<td>' + data[user].total + '</td></tr>' ;
            }
            _html += '</table>' ;
            $('#'+id_tag).html(_html);
        }
    });

}

$(document).ready(function(){
    var cls = ${c.siteadmin_clusters} ;
    var hostname, tag_prefix, jobs_tag, table_tag ;

    for(var i = cls.length -1; i>=0; i--){
        hostname = cls[i] ;
        tag_prefix = hostname.replace( /\./g , '-1-');  // replacing '.' with '-1-'
        jobs_tag = tag_prefix + '_jobs';
        table_tag = tag_prefix + '_table';

        generateLoadTable(hostname, table_tag) ;
        drawClusterUserJobs(hostname, jobs_tag);
    };
});

</script>



<%def name="js()">
    ${parent.js()}
    ${self.js_link("https://www.google.com/jsapi")}
</%def>

